package net.sinmetal.twitcockpit.controller.cockpit.schedule;

import net.sinmetal.twitcockpit.service.AutoPostService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.controller.validator.Validators;
import org.slim3.util.RequestMap;

/**
 * CockpitScheduleDeleteController
 *
 * @author Sinmetal
 *
 */
public class DeleteController extends Controller {

    private AutoPostService service = new AutoPostService();

    /**
     * 定期Postを削除する
     */
    @Override
    public Navigation run() throws Exception {
        final String TOP_PAGE = "/";
        RequestMap map = new RequestMap(request);
        if (!validate(map)) {
            return forward(TOP_PAGE);
        }

        int hour = Integer.valueOf((map.get("hour").toString()));
        int minute = Integer.valueOf((map.get("minute").toString()));
        String id = map.get("id").toString();
        service.deleteAutoPostVar(hour, minute, id);
        return redirect(TOP_PAGE);
    }

    /**
     * validate
     *
     * @param requestMap
     * @return
     */
    private boolean validate(RequestMap requestMap) {
        Validators v = new Validators(requestMap);
        v.add("id", v.required());
        v.add("hour", v.required(), v.integerType(), v.longRange(0, 23));
        v.add("minute", v.required(), v.integerType());
        return v.validate();
    }
}
